#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# To enable all, uncomment following line
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic -ffloat-store
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

include /usr/share/dpkg/default.mk

CFLAGS ?= $(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS ?= $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS ?= $(shell dpkg-buildflags --get LDFLAGS)
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -Wall
endif

ifeq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CC = gcc
else
CC = $(DEB_HOST_GNU_TYPE)-gcc
endif

SIMD_BROKEN_ARCHS = x32

ifeq (,$(filter $(DEB_HOST_ARCH), $(SIMD_BROKEN_ARCHS)))
  DISABLE_SIMD=
else
  DISABLE_SIMD=--without-simd
endif

%:
	dh $@ --parallel --with autoreconf -O--dbgsym-migration='libjpeg-turbo-progs-dbg (<< 1.5.0-1~), libjpeg62-turbo-dbg (<< 1.5.0-1~), libturbojpeg0-dbg (<< 1.5.0-1~)'

override_dh_auto_configure:
	dh_auto_configure -- --with-build-date=$(DEB_VERSION) $(DISABLE_SIMD)

override_dh_auto_build:
	dh_auto_build -- V=1
	$(MAKE) -C debian/extra CC=$(CC) CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" CPPFLAGS="$(CPPFLAGS)" V=1

override_dh_auto_clean:
	$(MAKE) -C debian/extra clean
	if [ -e Makefile ]; then $(MAKE) testclean; fi
	dh_auto_clean
	rm -f simd/jsimdcfg.inc

override_dh_auto_install:
	dh_auto_install
	# Install extra utilities
	$(MAKE) -C debian/extra install prefix=/usr DESTDIR=$(CURDIR)/debian/tmp
	# Install manual page for tjbench.1
	install -m 644 debian/tjbench.1 $(CURDIR)/debian/tmp/usr/share/man/man1/
	# Fix jconfig.h for MultiArch
	rm -f debian/tmp/usr/include/jconfig.h
	mkdir -p debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)
	sed -e "s/#\(undef\|define\) HAVE_\(LOCALE\|\(STD\(DEF\|LIB\)\)\)_H 1//g" \
		jconfig.h > debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/jconfig.h
	mkdir -p debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/
	sed -e "s/@VERSION@/$(VERSION)/;s/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/" \
		debian/libjpeg.pc.in \
		> debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/libjpeg.pc

override_dh_install:
	dh_install -X.la -X/usr/share/doc --fail-missing

override_dh_installchangelogs:
	dh_installchangelogs -plibjpeg62-turbo-dev change.log
	dh_installchangelogs -Nlibjpeg62-turbo-dev
